const { Controller } = require("egg");
const fs = require("fs")
const path = require("path")
const pump = require("mz-modules/pump")

class uploadController extends Controller {
    //     //上传图片
    async index() {
        try {
            const stream = await this.ctx.getFileStream() //获取文件流
            const fileName = new Date().getTime() + path.extname(stream.filename).toLowerCase(); //重写文件名
            const target = path.join(this.config.baseDir, "app/public/uploads", fileName) //目标文件夹
            const writeStream = fs.createWriteStream(target) //创建写入文件流
            await pump(stream, writeStream) //写入文件
            this.ctx.body = {
                code: 20000,
                data: {
                    name: fileName,
                    file: `http://127.0.0.1:7001/uploads/${fileName}`
                }
            }
        } catch (error) {
            return false
        }
    }
}

module.exports = uploadController